import { Injectable } from '@angular/core';
import { Loading, LoadingController,ToastController,Toast, AlertController } from "ionic-angular";
/*
  Generated class for the UtilsProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class UtilsProvider {
  
  constructor(private loadingCtrl:LoadingController,private toastCtrl: ToastController, private alertCtrl:AlertController) {
  };

  loader:Loading;
  /**
   * loading加载页面
   * 
   * @protected
   * @param {string} message 
   * 
   */
  showLoading(message:string):void{
      this.loader=this.loadingCtrl.create({
          content:message,
          //页面变化的时候自动关闭loading
          dismissOnPageChange:true,
          showBackdrop: true  //是否显示遮罩层
      });
      this.loader.present();
      // return loader;
  }
  /**
   * loading隐藏页面
   * 
   * @protected
   * @param {loader} 正在加载的loader 
   * 
   */
  hideLoading() {
      if (this.loader) {
          this.loader.dismiss();
      }
  }
  /**
   * 消息快速提示  2s
   * 
   * @protected
   * @param {string} message 
   * 
   */
  presentLoadingText(message:string) {
      let loading = this.loadingCtrl.create({
        spinner: 'hide',
        content: message
      });
      loading.present();
      setTimeout(() => {
        loading.dismiss();
      }, 2000);
    }

  /**
   * 通用的展示 toast 的组件
   * 
   * @protected
   * @param {string} message 
   * @returns {Toast} 
   */
  showToastTop(message: string): Toast {
      let toast = this.toastCtrl.create({
          message: message,
          duration: 2000, //默认展示的时长
          position: 'top'
      });
      toast.onDidDismiss(() => {
          console.log('Dismissed toast');
      });
      toast.present( );
      return toast;
  }
  /**
   * 一个按钮处理
   * @param title  标题
   * @param subTitle  副标题
   * @param message   详细信息
   * @param buttonTit button的显示文字
   * buttons: ['Dismiss']
   */
  presentAlert(title:string, message:string, buttonTit:string) {
      let alert = this.alertCtrl.create({
          title: title,
          message: message,
          //快捷写法
          // buttons: ['Dismiss'] 
          //自定义button文字写法  
          buttons: [{ 
              text:buttonTit,
              role: 'cancel',
              handler: () => {}
          }]  
      });
      alert.present();
  }
  /**
   * 两个按钮的处理
   * @param title 
   * @param message 
   * @param btnLeft 左边按钮的文字
   * @param btnRight 右边按钮的文字
   */
  presentConfirm(title:string, message:string, btnLeft:string, btnRight:string) {
      let alert = this.alertCtrl.create({
        title: title,
        message: message,
        buttons: [
          {
            text: btnLeft,
            role: 'cancel',
            handler: () => {
              console.log('Cancel clicked');
            }
          },
          {
            text: btnRight,
            handler: () => {
              console.log('Confirm clicked');
            }
          }
        ]
      });
      alert.present();
  }

  presentAsynHandler(title:string, message:string, btnTitle:string) {
      let alert = this.alertCtrl.create({
          title: title,
          buttons: [{
            text: btnTitle,
            handler: () => {
              // user has clicked the alert button
              // begin the alert's dismiss transition
              let navTransition = alert.dismiss();
        
              // start some async method
              // someAsyncOperation().then(() => {
                // once the async operation has completed
                // then run the next nav transition after the
                // first transition has finished animating out
        
                navTransition.then(() => {
                  // this.nav.pop();
                });
              // });
              return false;
            }
      }]
  });
      
      alert.present();
  }

}
